﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.IEArrangeB;
using StartsEAS_API.Controllers.ScheduleContor;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.EArrangeContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class EArrangeController : ControllerBase
    {
        private readonly IEArrangeBLL _eArrangeBll;
        public EArrangeController(IEArrangeBLL eArrangeBll) {
            _eArrangeBll = eArrangeBll;
        }
        /// <summary>
        /// 查询考试安排表信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllEArrange() {
            try
            {
                return _eArrangeBll.GetAllEArrange();
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllEArrange服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加一条考试安排
        /// </summary>
        /// <param name="token">教务处token</param>
        /// <param name="data">考试安排信息</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils addEArrange(string token, Earrange data) {
            try
            {
                return _eArrangeBll.addEArrange(token,data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"addEArrange服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 修改考试安排
        /// </summary>
        /// <param name="token">教务处token</param>
        /// <param name="data">考试安排信息</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils updateEArrange(string token, Earrange data) {
            try
            {
                return _eArrangeBll.updateEArrange(token,data);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"updateEArrange服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询考试安排信息
        /// </summary>
        /// <param name="token">学生或老师、教务处token</param>
        /// <param name="classId">老师和教务处所查班级的编号</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetEArrangesByToken(string token, string classId) {
            try
            {
                return _eArrangeBll.GetEArrangesByToken(token,classId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetEArrangesByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
